package com.wnhz.work.jy.adapter;

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ListView;
import android.widget.TextView;

import com.wnhz.work.jy.R;
import com.wnhz.work.jy.bean.OrderBean;

import java.util.List;

/**
 * Created by Administrator on 2016/9/12 0012.
 */
public class AllOrderAdapter extends BaseAdapter {
    private Context context;
    private List<OrderBean> orderList;
    private View.OnClickListener listener;
    private OrderDetailsAdapter adapter;

    public AllOrderAdapter(Context context, List<OrderBean> orderList, View.OnClickListener listener) {
        this.context = context;
        this.orderList = orderList;
        this.listener = listener;
    }

    @Override
    public int getCount() {
        return orderList.size();
    }

    @Override
    public Object getItem(int position) {
        return orderList.get(position);
    }

    @Override
    public long getItemId(int position) {
        return position;
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        AllOrderViewHolder viewHolder = new AllOrderViewHolder();
        if (convertView == null) {
            convertView = LayoutInflater.from(context).inflate(R.layout.all_order_item, null);
            viewHolder.tv_order_number = (TextView) convertView.findViewById(R.id.tv_order_number);
            viewHolder.tv_type = (TextView) convertView.findViewById(R.id.tv_type);

            viewHolder.tv_logistics = (TextView) convertView.findViewById(R.id.tv_logistics);//追踪物流
            viewHolder.tv_evaluate = (TextView) convertView.findViewById(R.id.tv_evaluate);//评价
            viewHolder.lay1 = convertView.findViewById(R.id.lay1);

            viewHolder.listView = (ListView) convertView.findViewById(R.id.listView);
            viewHolder.lay = convertView.findViewById(R.id.lay);
            viewHolder.view = convertView.findViewById(R.id.view);
            convertView.setTag(viewHolder);
        } else {
            viewHolder = (AllOrderViewHolder) convertView.getTag();
        }
        viewHolder.tv_order_number.setText("订单编号：" + orderList.get(position).getOrderNo());


        viewHolder.tv_logistics.setOnClickListener(listener);//追踪物流
        viewHolder.tv_logistics.setTag(position);
        viewHolder.tv_evaluate.setOnClickListener(listener);//评价
        viewHolder.tv_evaluate.setTag(position);
        adapter = new OrderDetailsAdapter(context,orderList.get(position).getGoodsInfo(),listener);
        viewHolder.listView.setAdapter(adapter);

        viewHolder.lay.setOnClickListener(listener);
        viewHolder.lay.setTag(position);
        viewHolder.view.setOnClickListener(listener);
        viewHolder.view.setTag(position);

        String start = orderList.get(position).getOrderState();
        if (start != null && !"".equals(start)){
            switch (Integer.parseInt(start)){
                case 0:
                    viewHolder.tv_logistics.setVisibility(View.VISIBLE);
                    viewHolder.tv_evaluate.setVisibility(View.VISIBLE);
                    viewHolder.tv_evaluate.setText("付款");
                    viewHolder.tv_logistics.setText("取消订单");
                    viewHolder.tv_type.setText("待付款");
                    viewHolder.lay1.setVisibility(View.VISIBLE);
                    break;
                case 1:
                    viewHolder.tv_logistics.setVisibility(View.INVISIBLE);
                    viewHolder.tv_evaluate.setVisibility(View.INVISIBLE);
                    viewHolder.tv_logistics.setVisibility(View.INVISIBLE);
                    viewHolder.tv_type.setText("待发货");
                    viewHolder.lay1.setVisibility(View.GONE);
                    break;
                case 2:
                    viewHolder.tv_logistics.setVisibility(View.VISIBLE);
                    viewHolder.tv_evaluate.setVisibility(View.VISIBLE);
                    viewHolder.tv_evaluate.setText("确认签收");
                    viewHolder.tv_logistics.setText("查看物流");
                    viewHolder.tv_type.setText("待收货");
                    viewHolder.lay1.setVisibility(View.VISIBLE);
                    break;
                case 3:
                    viewHolder.tv_logistics.setVisibility(View.VISIBLE);
                    viewHolder.tv_evaluate.setVisibility(View.VISIBLE);
                    viewHolder.tv_evaluate.setText("删除订单");
                    viewHolder.tv_logistics.setText("查看物流");
                    viewHolder.tv_type.setText("待评价");
                    viewHolder.lay1.setVisibility(View.VISIBLE);
                    break;
                case 4:
                    viewHolder.tv_logistics.setVisibility(View.VISIBLE);
                    viewHolder.tv_evaluate.setVisibility(View.VISIBLE);
                    viewHolder.tv_evaluate.setText("删除订单");
                    viewHolder.tv_logistics.setText("查看物流");
                    viewHolder.tv_type.setText("已完成");
                    viewHolder.lay1.setVisibility(View.VISIBLE);
                    break;
                case 5:
                    viewHolder.tv_logistics.setVisibility(View.VISIBLE);
                    viewHolder.tv_evaluate.setText("删除订单");
                    viewHolder.tv_logistics.setVisibility(View.INVISIBLE);
                    viewHolder.tv_type.setText("已取消");
                    viewHolder.lay1.setVisibility(View.VISIBLE);
                    break;
                case 6:
                    viewHolder.tv_logistics.setVisibility(View.VISIBLE);
                    viewHolder.tv_evaluate.setText("删除订单");
                    viewHolder.tv_evaluate.setVisibility(View.VISIBLE);
                    viewHolder.tv_logistics.setText("查看物流");
                    viewHolder.tv_type.setText("退款");
                    viewHolder.lay1.setVisibility(View.VISIBLE);
                    break;
            }
        }


        return convertView;
    }

    class AllOrderViewHolder {
        TextView tv_order_number;
        TextView tv_type;
        TextView tv_logistics;//追踪物流
        TextView tv_evaluate;//评价
        View lay1;
        ListView listView;
        View lay,view;
    }
}
